package com.mall.common.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.bean.SearchFilter.Operator;
import com.mall.common.domain.content.ArticleCategoryDomain;
import com.mall.common.domain.content.ArticleDomain;
import com.mall.common.exception.NoticeException;
import com.mall.common.service.ArticleCategoryService;
import com.mall.common.service.ArticleService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

/**
 * 后台Action类 - 文章
 */
@Controller
@RequestMapping("/admin/system/article")
public class ArticleController extends BaseController{
	@Resource private FreeMarkerConfigurer freeMarkerConfigurer;
	@Resource private ArticleService articleService;
	@Resource private ArticleCategoryService article_categoryService;
	/**
	 * 页面跳转
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(String parentMenuNo, HttpServletRequest request, Model model){
		model.addAttribute("parentMenuNo", parentMenuNo);
		return "/admin/system/article/main";
	}

	@RequestMapping(value="/list")
	public String list(Model model){
		return "/admin/system/article/list";
	}

	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(ArticleCategoryDomain category, Pager pager, HttpServletRequest request){
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
		if(category != null && category.getId() != null){
			category = article_categoryService.find(category.getId());
			if(category != null){
				filters.add(new SearchFilter("category.code", Operator.LIKE, category.getCode()));
			}
		}
		pager = articleService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	/**
	 * 跳转到新增页面
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String input(String parentMenuNo, Long catId, Model model){
		if(catId != null){
			ArticleCategoryDomain cate = article_categoryService.find(catId);
			if(cate != null){
				model.addAttribute("cate", cate);
			}
		}
		model.addAttribute("parentMenuNo", parentMenuNo);
		return "/admin/system/article/input";
	}
	
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "id", message = "文章不存在")	
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit(Long id, String parentMenuNo, Model model){
		ArticleDomain article = articleService.find(id);
		ArticleCategoryDomain category = article.getCategory();
		if(category == null){
			throw new NoticeException("文章不存在");
		}
		model.addAttribute("article", article);
		model.addAttribute("parentMenuNo", parentMenuNo);
		return "/admin/system/article/input";
	}

	@Validations(
		requiredFields = {
				@RequiredFieldValidator(fieldName = "category.id", message = "文章分类不存在")	
		},
		requiredStrings = {
			@RequiredStringValidator(fieldName = "title", message = "标题不能为空"),
			@RequiredStringValidator(fieldName = "author", message = "作者不能为空")
		}
	)
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String save(ArticleDomain article, String parentMenuNo, Model model){
		ArticleCategoryDomain category = article_categoryService.find(article.getCategory().getId());
		if(category == null){
			throw new NoticeException("文章不存在");
		}
		if(StringUtils.isNotBlank(article.getTitleImg())){
			article.setHasTitleImg(true);
		}else{
			article.setHasTitleImg(false);
		}
		articleService.persist(article);
		model.addAttribute("refresh", true);
		model.addAttribute("parentMenuNo", parentMenuNo);
		return CLOSEANRELOAPARENT;
	}
	
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "文章不存在")	
		}
	)
	@RequestMapping(value="/doPublish", method=RequestMethod.POST)
	@ResponseBody
	public String doPublish(Long[] ids, HttpServletResponse response) {
		articleService.pubish(ids);
		return ResponseUtils.renderJSON(successMessage, response);
	}

	/**
	 * 修改文章
	 * @return
	 */
	@Validations(
		requiredFields = {
				@RequiredFieldValidator(fieldName = "id", message = "文章不存在"),
				@RequiredFieldValidator(fieldName = "category.id", message = "文章分类不存在")	
		},
		requiredStrings = {
			@RequiredStringValidator(fieldName = "title", message = "标题不能为空"),
			@RequiredStringValidator(fieldName = "author", message = "作者不能为空")
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(ArticleDomain article, String parentMenuNo, Model model){
		ArticleCategoryDomain cate = article_categoryService.find(article.getCategory().getId());
		if(cate == null){
			throw new NoticeException("文章分类不存在");
		}
		article.setIsPublication(false);
		articleService.updateByUpdater(article);
		model.addAttribute("refresh", true);
		model.addAttribute("parentMenuNo", parentMenuNo);
		return CLOSEANRELOAPARENT;
	}
	
	// 删除
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "ids非法")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response) throws Exception {
		articleService.remove(ids);
		return ResponseUtils.renderJSON(successMessage, response);
	}

}
